﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

using YEngineLib;
using YEngineLib.Graphics;
using YEngineLib.Input;
using YEngineLib.State;
using YEngineLib.Util;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using System.Threading;
using Spyfall.Objects;

namespace Spyfall.States
{
    class CreditsState : YBaseGameState
    {
        public static int strlen(string s)
        {
            if (s == null)
                return 0;
            int length = 0;
            foreach (char c in s) length++;
            return length;
        }

        //States
        GamePadState currentGamePadState;
        GamePadState previousGameState;
        //Fonts
        SpriteFont spriteFont;
        //Which controller the Player is using
        public PlayerIndex playerIndex;
        //objectManager
        private Managers.ObjectManager objectManager;
        //screenPosition;
        Vector2 screenPosition;
        //Scroll
        float scroll;
        //Make my Timer
        float fElapsedTime, Timer;

        int screenHeight, screenWidth;

        public CreditsState()
        {
            objectManager = new Managers.ObjectManager();
            screenPosition = new Vector2(10.0f, 30.0f);
            screenWidth = 1024;
            screenHeight = 768;
            scroll = screenHeight + 10;
            fElapsedTime = 0.125f; // (float)YEngine.Time.GameTime.ElapsedGameTime.TotalMilliseconds;
            Timer = 0.0f;
        }

        public override void LoadContent()
        {
            LoadPercentage = 100.0f;
            spriteFont = YEngine.Game.Content.Load<SpriteFont>("Fonts/SpriteFont1");
        }

        public override void Input()
        {
            previousGameState = currentGamePadState;
            currentGamePadState = GamePad.GetState(playerIndex);

            //Keyboard shortcut to return back to the mainmenu
            if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter) ) //|| ButtonState.Pressed && previousGameState.Buttons.B != ButtonState.Pressed)
            {
                YEngine.State.ChangeState("MenuState");
                return;
            }
        }

        public override void Update()
        {
            Timer += (fElapsedTime * 40.0f);

            if (Timer > 1.0f)
            {
                Timer = 0.0f;
                scroll -= 1;
            }
            if (scroll < -10)   //adjust the size due to the size of credits
                scroll = screenHeight + 10;

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                YEngine.Game.Exit();
                objectManager.UpdateObjects((float)YEngine.Time.GameTime.ElapsedGameTime.TotalSeconds);
            }
        }

        public override void Draw()
        {
            //SpriteBatch spriteBatch;
            YEngine.Graphics.SpriteBatch.Begin();
            YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Credits Prototype",                new Vector2((screenWidth * 0.5f) - ((strlen("Credits Prototype") * 2.1f) * 2),               scroll), Color.White);
            YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Game Design By: TEST TEST",        new Vector2((screenWidth * 0.5f) - ((strlen("Game Design By: TEST TEST") * 2.1f) * 2),       scroll + 40), Color.White);
            YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Game Development By: TEST TEST",   new Vector2((screenWidth * 0.5f) - ((strlen("Game Development By: TEST TEST") * 2.1f) * 2),  scroll + 60), Color.White);
            YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "Special Thanks to: DesNuts",
                                                                                                    new Vector2((screenWidth * 0.5f) - ((strlen("Special Thanks to: DesNuts") * 2.1f) * 2),
                                                                                                                                                                                        scroll + 80), Color.White);
           //NOT HAPPY WITH MY MID, update later.
            //(screen width * .5) - (strlen * width of letter * .5)
            //YText.Draw(spriteBatch, spriteFont, "HELLO WORLD", new Vector2(50.0f, 100.0f), 2.0f, Color.Yellow, Color.WhiteSmoke, new Vector2(50.5f, 100.5f), 1, 0);
            YEngine.Graphics.SpriteBatch.End();
        }
    }
}
